<?php
namespace Home\Controller;
/**
 * 我的抵押
 * @author Administrator
 *
 */
class MydomortController extends HomeBaseController
{
    public function index(){
        $page=I('page',1);
        $size=10;
        $start=($page-1)*$size;
        $mortcon['uid']=session('uid');
        $mortcon['expir_date']=date("Y-m-d").' 00:00:00';
        
        $mortmoy=M('UserMortdetail')->field(array("interest","credit_limit"))->where($mortcon)->select();
        $this->assign("daymort",$mortmoy['interest']+$mortmoy['credit_limit']);
        
        $domCon['uid']=session('uid');
        $domCon['is_mortage']=2;
        $mortCount=M('UserDomains')->where($domCon)->count();
        $this->assign("mortCount",$mortCount);
        
        $avail_money=M("UserAssets")->field('avail_money')->where("uid=".session('uid'))->select();
        $this->assign("avail_money",$avail_money[0]['avail_money']);
        
        
        $DomainType = M('DomainType');
        $domTypes = $DomainType->select();
        $this->assign('domTypes', $domTypes);
        
        $category=I('category','');
        $this->assign("category",$category);
        
        $status=I('status','');
        $this->assign("status",$status);
        
        if($category!=''){
            $condi=' and a.domaintyid='.$category;
            $condic=' and domaintyid='.$category;
        }
        if($status!=''){
            $condi+=' and a.status='.$status;
            $condic+=' and status='.$status;
        }
        $dao=M('UserMortdetail');
        $sql="SELECT a.id,b.name,c.domain,a.credit_limit,a.interest,a.penalty,a.expir_day,a.create_date,a.expir_date,a.status,(a.credit_limit+a.interest+a.penalty) AS total FROM dd_user_mortdetail a LEFT JOIN dd_domain_type b ON a.domaintyid=b.id left join dd_user_domains c on a.domainid=c.id WHERE a.uid=".session('uid').$condi.' order by a.create_date limit '.$start.','.$size;
        $list = $dao->query($sql);
       
        $this->assign("list",$list);
        $count =$dao->where(' uid='.session('uid').$condic)->count();
        
        $this->assign('page', $page);
        $this->assign('pageNum', $count%$size);
        $this->assign('count', $count);
        $this->display('mydomort/index');
    }
    
    public function repayment(){
        if(session('uid')==null){
            $this->ajaxReturn(array('status'=>'2','msg' => '请选去登录!'));//0错误，1成功
            exit;
        }
        $authpwd=I('authpwd','');
        $transpasswd=M('user')->field('transpasswd')->find(session('uid'));
        if($transpasswd['transpasswd']!=md5($authpwd)){
            $this->ajaxReturn(array('status'=>'2','msg' => '交易密码错误!'));//0错误，1成功
            exit;
        }
        $id = I('id',0);
        $mort=M('UserMortdetail')->field('id,expir_date,credit_limit,interest,domainid')->find($id);
        if(empty($mort)){
            $this->ajaxReturn(array('status'=>'2','msg' => '还款记录不存在!'));//0错误，1成功
            exit;
        }
        
        /**
         * 计算罚息 若有逾期
         * @var unknown
         */
        $Date_1=date("Y-m-d");
        $Date_2=date("Y-m-d",strtotime($mort['expir_date']));
        $d1=strtotime($Date_1);//当前日期
        $d2=strtotime($Date_2);//到期日期
        $Days=round(($d1-$d2)/3600/24);
        if($Days>0 && $mort['status']!=3){//逾期天数大于0，说明已经逾期
            //2%/30天*逾期天数*1.4倍 逾期时候计算罚息
            $faxi=(($mort['credit_limit']*0.02)/30)*(1.4*$Days);
            $faxi=(double)(sprintf("%.2f", $faxi));
        }else{
            $faxi=0.0;
        }
       $mort['interest']=($faxi+$mort['interest']+$mort['credit_limit']);
        /**
         * 操作用户资金账户
         */
        $userAss=M('UserAssets')->where('uid='.session('uid'))->select();
        if($userAss[0]['avail_money']<$mort['interest']){//余额不足
            $this->ajaxReturn(array('status'=>'2','msg' => '余额不足,请先去充值!'));//0错误，1成功
            exit;
        }
        if($userAss[0]['notpresent_money']>=$mort['interest']){
            M('UserAssets')->where('uid='.session('uid'))->setDec('notpresent_money',$mort['interest']);
        }else{
            M('UserAssets')->where('uid='.session('uid'))->setDec('notpresent_money',$userAss[0]['notpresent_money']);
            M('UserAssets')->where('uid='.session('uid'))->setDec('present_money',($mort['interest']-$userAss[0]['notpresent_money']));
        }
        M('UserAssets')->where('uid='.session('uid'))->setDec('avail_money',$mort['interest']);
        
        //插入平台手续费收入log
        $platPrflog=M('PlatformProfitlog');
        $platlog['money']=$mort['interest'];
        $platlog['comefr']=2;//来源,1=手续费，2=利息，3=罚息
        $platlog['userid']=session('uid');
        $platlog['descr']='ID为'.session('uid').'的用户还款，平台收入:'.$mort['interest'];
        $platlog['createdate']=date('y-m-d H:m:s',time());
        $platPrflog->data($platlog)->add();
        
        /**
         * 操作域名表及抵押表
         */
        M('UserMortdetail')->where('id='.$id)->setField(array('status'=>2,'penalty'=>$faxi));
        M('UserDomains')->where('id='.$mort['domainid'])->setField(array('is_mortage'=>1,'entrust'=>0));
        $this->ajaxReturn(array('status'=>'1','msg' => '还款成功!'));//0错误，1成功
        exit;
    }
    
}